package com.geek.javase.ch17.work.tcpwork.tcp1;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

/*
 *@author wangpeng
 *2025/11/4 9:33
 *@description
 * 需求1：客户端与服务端可以建立连接 ； 服务端接收一条消息，客户端发送一条消息
 */
public class TCPClient {
    public static void main(String[] args) {
        System.out.println("-------客户端--------");
        Socket socket = null;
        OutputStream os = null;
        PrintWriter pw = null;

        try{
            //1.创建客户端，连接服务器
            socket = new Socket(InetAddress.getLocalHost(), 8899);
            //2.写消息
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.println("你好，我是客户端！");
            pw.flush();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            //关闭连接
            if(socket != null){
                try{
                    socket.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
